package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Area.AreaInfoCriteria;
import com.deer.wms.base.system.model.Area.AreaInfoDto;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.busine.tactic.model.Upper.UpperTacticDetail;
import com.deer.wms.busine.tactic.service.UpperTacticDetailService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.model.ValidatorAreaInfoModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created by guo on 2019/11/21.
 */
@Api(description = "货区信息api接口")
@RestController
@RequestMapping("/area/infos")
public class AreaInfoController {

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private UpperTacticDetailService upperTacticDetailService;

    @ApiOperation(value = "新增货区信息", notes = "新增货区信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody AreaInfo areaInfo, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<AreaInfoDto> list1 = areaInfoService.findByCode(areaInfo.getAreaCode());
        List<AreaInfoDto> list2 = areaInfoService.findByName(areaInfo.getAreaName());

        if (list1.size() != 0) {
            throw new ServiceException(CommonCode.AREA_EXISTS, "此货区编码已存在");
        }
        if (list2.size() != 0) {
            throw new ServiceException(CommonCode.AREA_EXISTS, "此货区已存在");
        }

        String nowDate = DateUtils.getNowDateTimeString();
        areaInfo.setAddTime(nowDate);
        areaInfoService.save(areaInfo);

        return ResultGenerator.genSuccessResult();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/delete")
    @ApiOperation(value = "删除货区信息", notes = "删除货区信息")
    public Result delete(Integer id, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        AreaInfo areaInfo = areaInfoService.getById(id);
        //校验是否已经绑定上架策略，若绑定则不允许删除
        List<UpperTacticDetail> list = upperTacticDetailService.findByAreaCode(areaInfo.getAreaCode());
        if (list.size() != 0) {
            throw new ServiceException(CommonCode.AREA_UPPER_TACTIC_BOUND, "此货区已绑定上架策略");
        } else {
            areaInfoService.removeById(id);
        }
        return ResultGenerator.genSuccessResult();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @PostMapping("/update")
    @ApiOperation(value = "更新货区信息", notes = "更新货区信息")
    public Result update(@RequestBody AreaInfo areaInfo, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<AreaInfoDto> list = areaInfoService.findByCode(areaInfo.getAreaCode());
        if (list.size() == 0) {
            String nowDate = DateUtils.getNowDateTimeString();
            areaInfo.setAddTime(nowDate);
            areaInfoService.updateById(areaInfo);
        }
        if (list.size() == 1) {
            if (list.get(0).getAreaId().equals(areaInfo.getAreaId())) {
                List<AreaInfoDto> list1 = areaInfoService.findByName(areaInfo.getAreaName());
                if (list1.size() == 0) {
                    String nowDate = DateUtils.getNowDateTimeString();
                    areaInfo.setAddTime(nowDate);
                    areaInfoService.updateById(areaInfo);
                } else if (list1.size() == 1 && list1.get(0).getAreaId().equals(areaInfo.getAreaId())) {
                    String nowDate = DateUtils.getNowDateTimeString();
                    areaInfo.setAddTime(nowDate);
                    areaInfoService.updateById(areaInfo);
                } else {
                    throw new ServiceException(CommonCode.AREA_EXISTS, "此货区已存在");
                }
            } else if (!list.get(0).getAreaId().equals(areaInfo.getAreaId())) {
                throw new ServiceException(CommonCode.AREA_CODE_EXISTS, "此货区编码已存在");
            }
        }
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        AreaInfo areaInfo = areaInfoService.getById(id);
        return ResultGenerator.genSuccessResult(areaInfo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(AreaInfoCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AreaInfoDto> list = areaInfoService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/validator")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)
    })
    @ApiOperation(value = "验证货区信息重复", notes = "验证货区信息重复")
    public Result validatorAreaInfo(@RequestBody ValidatorAreaInfoModel model) {
        int count = areaInfoService.validatorChongfu(model);
        if (count > 0) {
            if (model.getAreaCode() != null) {
                return ResultGenerator.genSuccessResult(CommonCode.CODE_EXISTS);
            }
            if (model.getAreaName() != null) {
                return ResultGenerator.genSuccessResult(CommonCode.NAME_EXISTS);
            }
        }
        return ResultGenerator.genSuccessResult(CommonCode.NAME_CODE_NO_EXISTS);
    }
}
